package com.itheima.service;



import com.itheima.common.SnowFlake;
import com.itheima.mapper.admin.OrderMapper;
import com.itheima.mapper.client.AddressBookMapper;
import com.itheima.mapper.client.ShoppingCartMapper;
import com.itheima.mapper.client.UserMapper;
import com.itheima.pojo.admin.Pagination;
import com.itheima.pojo.client.OrderAddDto;
import com.itheima.pojo.client.OrderPagingVo;
import com.itheima.pojo.client.SelectAddressByIdVo;
import com.itheima.pojo.client.SelectUserById;
import com.itheima.pojo.client.orderPaginationVo.OrderDetail;
import com.itheima.pojo.client.orderPaginationVo.OrderPaginationVo;
import com.itheima.pojo.client.orderPaginationVo.OrderWaresAdd;
import com.itheima.pojo.client.selectShoppingCartAllVo.SelectShoppingCartAllVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ChenJunJie
 * @date 2022/6/18 17:23
 * 订单管理service层
 */
@Service
public class CilentOrderService {

    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AddressBookMapper addressBookMapper;


    //提交订单(添加订单)
    public boolean addOrder(OrderAddDto orderAddDto){

        //设置订单id
        long id = snowFlake.nextId();
        orderAddDto.setId(id+"");

        //设置订单号
        Date date = new Date();
        SimpleDateFormat sdfNumber = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowTime = sdfNumber.format(date);
        orderAddDto.setNumber(nowTime);

        //设置总金额
        List<SelectShoppingCartAllVo> shoppingCarts = shoppingCartMapper.selectShoppingCartAll(orderAddDto.getUserId());
        BigDecimal sum = new BigDecimal(0);
        for (SelectShoppingCartAllVo shoppingCart : shoppingCarts) {
            //获得每个菜的金额
            BigDecimal amount = shoppingCart.getAmount();
            //获得每个菜的份数
            Integer num = shoppingCart.getNumber();

            BigDecimal number = new BigDecimal(num+"");
            //将金额与份数相乘
            BigDecimal multiply = amount.multiply(number);
            //最后加在总数上
            sum = sum.add(multiply);
        }
        orderAddDto.setAmount(sum);

        //设置用户名
        SelectUserById user = userMapper.selectUserById(orderAddDto.getUserId());
        orderAddDto.setUsername(user.getUsername());

        //设置电话
        orderAddDto.setPhone(user.getPhone());

        //设置地址
        SelectAddressByIdVo address = addressBookMapper.selectAddressById(orderAddDto.getAddressBookId());
        orderAddDto.setAddress(address.getDetail());

        //设置收件人
        orderAddDto.setConsignee(address.getConsignee());


        int i = orderMapper.addOrder(orderAddDto);

        if (i>0){
            //获取当前的订单与菜品的关系集合
            List<OrderWaresAdd> list = new ArrayList<>();
            //遍历菜品获取元素
            for (SelectShoppingCartAllVo shoppingCart : shoppingCarts) {
                OrderWaresAdd orderWaresAdd =new OrderWaresAdd();
                long orderWaresId = snowFlake.nextId();
                orderWaresAdd.setId(orderWaresId+"");
                orderWaresAdd.setOrderId(id+"");
                orderWaresAdd.setNumber(shoppingCart.getNumber());
                orderWaresAdd.setWaresName(shoppingCart.getName());
                list.add(orderWaresAdd);
            }
            //新增关系
            orderMapper.orderWaresAdd(list);

            int delete = shoppingCartMapper.deleteShoppingCartByUserId(orderAddDto.getUserId());
            return delete > 0;
        }else {
            return false;
        }
    }

    //移动端查询订单分页
    public OrderPagingVo cilentorderPagination(Integer page, Integer pageSize,String userId){

        //通过用户id查询所有订单
        List<OrderPaginationVo> orderSelectAll = orderMapper.selectOrderAll(userId);

        //设置索引
        Integer index = (page-1) * pageSize;

        //查询订单分页
        List<OrderPaginationVo> orderPagination = orderMapper.cilentOrderPagination(index, pageSize,userId);



        //查询每一个订单
        for (OrderPaginationVo order : orderPagination) {

            //创建商品集合
            List<OrderDetail> list = new ArrayList<>();

            //通过每个订单的id查询其对应的订单商品关系
            List<OrderWaresAdd> orderWaresAdds = orderMapper.orderWaresSelectById(order.getId());

            //将关系中的商品名与个数添加到商品集合中
            for (OrderWaresAdd orderWaresAdd : orderWaresAdds) {
                if (orderWaresAdd.getOrderId().equals(order.getId())){
                    OrderDetail orderDetail = new OrderDetail();
                    orderDetail.setName(orderWaresAdd.getWaresName());
                    orderDetail.setNumber(orderWaresAdd.getNumber());
                    list.add(orderDetail);
                }
            }
            //存完商品后将其放入订单中
            order.setOrderDetails(list);
        }

        //需要返回的响应体对象
        OrderPagingVo op = new OrderPagingVo();
        op.setRecords(orderPagination);
        op.setTotal(orderSelectAll.size());
        op.setSize(pageSize);
        op.setCurrent(page);

        int total = op.getTotal();
        int size = op.getSize();

        op.setPages((int)Math.ceil((double) total / (double) size));

        return op;
    }

}
